package org.ikastola

import ikastola.Constants;

import org.springframework.dao.DataIntegrityViolationException
import org.springframework.transaction.annotation.Transactional;
import grails.plugins.springsecurity.Secured


@Secured(['ROLE_ADMIN'])
class GuardianController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [guardianInstanceList: Guardian.findAllByDeleted(Constants.NOT_DELETED,params), guardianInstanceTotal: Guardian.countByDeleted(Constants.NOT_DELETED)]
    }

    def create() {
        [guardianInstance: new Guardian(params), st: params.st]
    }

	@Transactional
    def save() {
		
		def guardianInstance = new Guardian(params)
		guardianInstance.setDeleted(Constants.NOT_DELETED)
		guardianInstance.setClassCreated(new Date())
        if (!guardianInstance.save(flush: true)) {
            render(view: "create", model: [guardianInstance: guardianInstance])
            return
        }
		
		if (params.st) {
			def student = Student.get(params.st)
			if (student) {
				student.setClassUpdated(new Date())
				student.addToGuardians(guardianInstance)
				student.save(flush: true)
			}
		}

        flash.message = message(code: 'default.created.message', args: [message(code: 'guardian.label', default: 'Guardian'), guardianInstance.id])

        redirect(action: "show", id: guardianInstance.id,  params: [st: params.st])
    }

    def show(Long id) {
        def guardianInstance = Guardian.get(id)
        if (!guardianInstance || Constants.DELETED.equals(guardianInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'guardian.label', default: 'Guardian'), id])
            redirect(action: "list")
            return
        }
        [guardianInstance: guardianInstance, st: params.st]
    }

    def edit(Long id) {
        def guardianInstance = Guardian.get(id)
        if (!guardianInstance || Constants.DELETED.equals(guardianInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'guardian.label', default: 'Guardian'), id])
            redirect(action: "list")
            return
        }

        [guardianInstance: guardianInstance]
    }

	@Transactional
    def update(Long id, Long version) {
        def guardianInstance = Guardian.get(id)
        if (!guardianInstance || Constants.DELETED.equals(guardianInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'guardian.label', default: 'Guardian'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (guardianInstance.version > version) {
                guardianInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'guardian.label', default: 'Guardian')] as Object[],
                          "Another user has updated this Guardian while you were editing")
                render(view: "edit", model: [guardianInstance: guardianInstance])
                return
            }
        }
        guardianInstance.properties = params
		guardianInstance.setClassUpdated(new Date())
        if (!guardianInstance.save(flush: true)) {
            render(view: "edit", model: [guardianInstance: guardianInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'guardian.label', default: 'Guardian'), guardianInstance.id])
        redirect(action: "show", id: guardianInstance.id)
    }

	@Transactional
    def delete(Long id) {
        def guardianInstance = Guardian.get(id)
        if (!guardianInstance || Constants.DELETED.equals(guardianInstance.getDeleted())) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'guardian.label', default: 'Guardian'), id])
            redirect(action: "list")
            return
        }

		def studentInstance = Student.findAllByGuardians(guardianInstance)
		if (studentInstance) {
			flash.message = "La persona a cargo no puede ser eliminada dado que est&aacute; asignada a un Alumno."
			redirect(action: "list")
			return
		}
		
        try {
			guardianInstance.setDeleted(Constants.DELETED)
			guardianInstance.setClassUpdated(new Date())
			guardianInstance.save(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'guardian.label', default: 'Guardian'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'guardian.label', default: 'Guardian'), id])
            redirect(action: "show", id: id)
        }
    }
}
